<?php
namespace Home\Controller;
use EasyWeChat\Factory;
/**
 * 
 * 微信处理
 */
class WechatController extends CommonController {
    
	/**
     * 配置参数.
     */
    protected $config;

    public function _initialize() {
    	$Model = M('wechat_conf');
    	$data = $Model->where('user_id='.$_GET['user_id'])->find();
        $this->config = array(
						    'app_id' => $data['appid'],
						    'secret' => $data['secret'],
						    'token'   => 'Uzp3cJ4z',
						    'aes_key' => '', 
						    'response_type' => 'array',

						    'log' => [
						        'level' => 'debug',
						        'file' => __DIR__.'/wechat.log',
						    ],
						);
        // var_dump($data);die;
    }

    public function index()
    {
    	$app = Factory::officialAccount($this->config);

    	$app->server->push(function ($message) {
    		switch ($message['MsgType']) {
		        case 'event':
		        	if ($message['Event']=="subscribe"||$message['Event']=="unsubscribe") {//关注事件
		        		$openid = $message['FromUserName'];
		        		$this->getUser($openid,$message['Event']);
		        	}
			        return $message['FromUserName'];
		            break;
		        case 'text':
		            return '收到文字消息';
		            break;
		        case 'image':
		            return '收到图片消息';
		            break;
		        case 'voice':
		            return '收到语音消息';
		            break;
		        case 'video':
		            return '收到视频消息';
		            break;
		        case 'location':
		            return '收到坐标消息';
		            break;
		        case 'link':
		            return '收到链接消息';
		            break;
		        // ... 其它消息
		        default:
		            return '收到其它消息';
		            break;
		    }

		    // return "您好！欢迎使用 EasyWeChat!";
		});

		$response = $app->server->serve();

		// 将响应输出
		$response->send();
    }

    public function getUser($openid=null,$type=null){
    	$app = Factory::officialAccount($this->config);
    	$openid = $_GET['openid'];
    	if($openid){
    		$Model = M('wechat_user');
    		$data = $Model->where(array('openid'=>$openid))->find();
    		if ($data) {
    			$subscribe = $type=="subscribe"?"1":"0";
    			$Model->where(array('openid'=>$openid))->save(array('subscribe'=>$subscribe,'updated_at'=>time()));
    		}else{
		    	$users = $app->user->select(array($openid));
		    	$this->save_user($users,$this->config['app_id']);
    		}
    	}else{
	    	$user = $app->user->list($nextOpenId = null);
	    	$users = $app->user->select($user['data']["openid"]);
	    	$this->save_user($users,$this->config['app_id']);
    	}
    }

    public function save_user($users,$appid){
    	$sql = "INSERT INTO ea_wechat_user (nickname,openid,subscribe,city,sex,province,country,headimgurl,subscribe_time,remark,groupid,appid,created_at,updated_at) VALUES";
    	$time = time();
    	foreach ($users["user_info_list"] as $key => $value) {
    		$sql .= "('".$value['nickname']."', '".$value['openid']."', '".$value['subscribe']."', '".$value['city']."', '".$value['sex']."', '".$value['province']."', '".$value['country']."', '".$value['headimgurl']."', '".$value['subscribe_time']."', '".$value['remark']."', '".$value['groupid']."', '".$appid."', '".$time."', '".$time."'),";
    	}
    	$sql = rtrim($sql, ',');
    	$Model = M();
    	$Model->query($sql);
    }
}
